package com.firemoss.magicbus.messaging
{
	import flash.events.Event;
	
	import mx.events.DynamicEvent;
	import mx.messaging.events.MessageEvent;

	/**
	 * Creates an event named {mediator.messageEvent}, populates it with either
	 * the message's body (if {mediator.bodyProperty} is set) or with values from the message's
	 * body object (if {mediator.bodyProperties} is set).  "bodyProperties" takes precedence.
	 */ 
	public class DefaultConsumerMessageConverter extends AbstractDefaultMessageConverter implements IConsumerMessageConverter
	{

		public function DefaultConsumerMessageConverter( messageAgentMediator : IMessageAgentMediator )
		{
			super( messageAgentMediator )
		}

		public function convert(mediator:IConsumerMediator, event:MessageEvent):Event
		{
			if ( mediator.messageEvent ) 
			{
				var note : DynamicEvent = new DynamicEvent( mediator.messageEvent )
				
				if ( mediator.bodyProperties )
				{
					for ( var item : String in mediator.bodyProperties )
					{
						note[ item ] = event.message.body[ item ]
					} 
				}
				else if ( mediator.bodyProperty )
				{
					note[ mediator.bodyProperty ] = event.message.body
				}
							
				return note
			}
			else
			{
				return null
			}
			
		}
		
	}
}